package com.oocl.im.gui;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.im.pojo.MsgPackage;
import com.oocl.im.pojo.Operation;
import com.oocl.im.util.JsonUtil;

public class RegisterFrame extends JFrame {
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JPasswordField psdPf1;
	private JButton rgtBtn;
	private JButton cancelBtn;
	private JLabel userName;
	private JLabel password;
	private JLabel password1;

	public RegisterFrame() throws HeadlessException {
		super();
		this.setSize(400, 280);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		init();
		addEvent();
		this.setVisible(true);
		this.setTitle("Welcome to login !");
	}

	private void init() {
		this.setLayout(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		psdPf1 = new JPasswordField();
		rgtBtn = new JButton("register");
		cancelBtn = new JButton("cancel");
		userName = new JLabel("UserName:");
		password = new JLabel("Password:");
		password1 = new JLabel("EnsurePsd:");
		nameTf.setBounds(120, 20, 230, 35);
		psdPf.setBounds(120, 80, 230, 35);
		psdPf1.setBounds(120, 140, 230, 35);
		rgtBtn.setBounds(120, 200, 90, 40);
		cancelBtn.setBounds(260, 200, 90, 40);
		userName.setBounds(30, 20, 90, 35);
		password.setBounds(30, 80, 90, 35);
		password1.setBounds(30, 140, 90, 35);
		this.add(nameTf);
		this.add(psdPf);
		this.add(psdPf1);
		this.add(rgtBtn);
		this.add(cancelBtn);
		this.add(userName);
		this.add(password);
		this.add(password1);
	}

	private void addEvent() {
		rgtBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String name = nameTf.getText();
				String password = psdPf.getText();
				String password1 = psdPf1.getText();
				if(password.equals(password1)){
				try {
					Socket socket = new Socket("127.0.0.1", 8400);
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(socket.getInputStream()));
					PrintWriter writer = new PrintWriter(socket
							.getOutputStream(), true);
					MsgPackage msg = new MsgPackage(Operation.REGISTER, name,
							null, password, null, null);
					String msgJson = JsonUtil.objectToString(msg);
					writer.println(msgJson);
					String msgJson2 = reader.readLine();
					MsgPackage msg2 = JsonUtil.stringToObject(msgJson2);
					if(!msg2.getContent().equals("regist success!")){
					new InfoFrame(msg2.getContent());}
					else{
						new InforFrameClose(msg2.getContent());
					}
					socket.close();
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				}else{
					new InfoFrame("The password is inconsistent !");
				}
			}
		});
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("cancel register...");
				System.exit(0);

			}
		});
	}

	public static void main(String[] args) {
		new RegisterFrame();
	}
}
